home *** CD-ROM | disk | FTP | other *** search
Wrap
Text File | 1994-09-13 | 13.4 KB | 245 lines | [ TEXT/QKSA]
DemoLib name: #WireList superclass: Environment@#List flags: 0x80000 category: #'WireList Example' classInstanceVars: nil namedInstanceVars: #(first second) classPoolVars: nil pools: nil structures: nil storageSize: 0 ! (DemoLib@#WireList) metaclass description: (Text from: 'This class holds the data structures for the WireListModel. It''s protocol supports the algorithmics for the WireListModel.' styleRuns:((ScrapStyle basicNew: 0) storageSize: 22; storageFromHexString: '000100000000000C000A000100000009000000000000'))! DemoLib@#WireList compileMethodSource: ( Text from: 'add: aPoint " add a point to the end of the list" (self setToEnd) nextPut: aPoint. ' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B000900040000000900000000000000000010000B000900040000000999996666000000000035000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'add:') protocolCategory: #'accessing'.! (DemoLib@#WireList methodAt: #'add:') description: ('Adds a point to the list').! DemoLib@#WireList compileMethodSource: ( Text from: 'second "second element accessor; not used in WireListModel" ^self position:1; next ' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B00090004000000090000000000000000000C000B000900040000000999996666000000000040000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'second') protocolCategory: #'accessing'.! DemoLib@#WireList compileMethodSource: ( Text from: 'first "First element accessor; not used in WireListModel" ^self reset; peek' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B00090004000000090000000000000000000B000B00090004000000099999666600000000003E000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'first') protocolCategory: #'accessing'.! (DemoLib@#WireList methodAt: #'first') description: ('Access the first element in list. NOT USED for WireListModel').! DemoLib@#WireList compileMethodSource: ( Text from: 'drawNode: p " draws little circles 5 pixels diam. to represent points selected" (Oval top: ((p y) - 5) left: ((p x) - 5) bottom: ((p y) + 5) right: ((p x) + 5)) frame.' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B000900040000000900000000000000000012000B000900040000000999996666000000000055000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'drawNode:') protocolCategory: #'drawing'.! (DemoLib@#WireList methodAt: #'drawNode:') description: ('This draws the node shape that represents the wire point. (used for 2nd point on)').! DemoLib@#WireList compileMethodSource: ( Text from: 'shorten "Try random changes in the routing order. Keep only changes that shorten the length." | minLength i j | "set the current length to the first guess at the minimum" minLength := self length. "Randomly generate integers and exchange them two at a time. Keep the order with the smallest length on each trial" 100 "<-- Arbitrary number guess of trials to reach min. This should really be scaled to change with number of elements; e.g. might try (self size * 20) instead" timesRepeat: [i := ((Float random * self size) truncate + 1)asInteger. " see WireListModel class NOTES caveats" j := ((Float random * self size) truncate + 1)asInteger. self exchange: i and: j. self length < minLength ifTrue: [minLength := self length] ifFalse: [self exchange: i and: j]]' styleRuns:((ScrapStyle basicNew: 0) storageSize: 222; storageFromHexString: '000B00000000000B00090004000000090000000000000000000A000B000900040000000999996666000000000062000B00090004000000090000000000000000007A000B0009000400000009999966660000000000B4000B0009000400000009000000000000000000D2000B000900040000000999996666000000000149000B00090004000000090000000000000000014E000B0009000400000009999966660000000001EF000B00090004000000090000000000000000023C000B000900040000000999996666000000000264000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'shorten') protocolCategory: #'calculating'.! (DemoLib@#WireList methodAt: #'shorten') description: ('The primary algorithm to compute the shortest path between points. This is a sotchastic approx. or Monte Carlo based algorithm, and cannot be guaranteed to produce true minima in all cases.').! DemoLib@#WireList compileMethodSource: ( Text from: 'length "Answer the length of the wire." | total previous | total := 0. self position: 0. previous := self next. self do: [:next | total := total + (((previous x - next x) squared) + ((previous y - next y) squared)) squareRoot. previous := next]. ^total ' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B000900040000000900000000000000000008000B000900040000000999996666000000000028000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'length') protocolCategory: #'calculating'.! (DemoLib@#WireList methodAt: #'length') description: ('This computes the length of the wire.').! DemoLib@#WireList compileMethodSource: ( Text from: 'exchange: index1 and: index2 "Exchange the elements at the first and second indexes." | temp | temp := self at: index1. self at: index1 put: (self at: index2). self at: index2 put: temp ' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B00090004000000090000000000000000001F000B000900040000000999996666000000000057000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'exchange:and:') protocolCategory: #'accessing'.! DemoLib@#WireList compileMethodSource: ( Text from: 'drawOn: wPort "This is the primary routine used to draw the nodes and connecting lines representing the wire." | p pBnds| " make a copy of the bounds because some bounds methods are destructive" pBnds := wPort bounds. " set up the clipping region to be inside the framed rectangle; try commenting this out to see what happens" wPort clipRect: (Rectangle top: pBnds top + 34 left: pBnds left + 14 bottom: pBnds bottom -14 right: pBnds right - 14 ). "check that there is something to draw" ((self size) > 0) ifFalse: [ ^nil ]. " draw the black node for the first" self drawNodeFirst: (self at: 1). wPort movePenTo: (self at: 1). "now draw the remaining ones" 2 to: (self size) do: [ :i | p := (self at: i). wPort drawLineToX: (p x) Y: (p y). self drawNode: p. ].' styleRuns:((ScrapStyle basicNew: 0) storageSize: 262; storageFromHexString: '000D00000000000B000900040000000900000000000000000013000B000900040000000999996666000000000077000B000900040000000900000000000000000091000B0009000400000009999966660000000000D9000B0009000400000009000000000000000000FE000B000900040000000999996666000000000171000B000900040000000900000000000000000221000B000900040000000999996666000000000249000B000900040000000900000000000000000280000B0009000400000009999966660000000002A4000B0009000400000009000000000000000002FE000B00090004000000099999666600000000031B000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'drawOn:') protocolCategory: #'drawing'.! (DemoLib@#WireList methodAt: #'drawOn:') description: ('Draws the points and connecting lines.').! DemoLib@#WireList compileMethodSource: ( Text from: 'drawNodeFirst: p " same as drawNode: but fills the region; it is used only for first point" (Oval top: ((p y) - 5) left: ((p x) - 5) bottom: ((p y) + 5) right: ((p x) + 5)) fill: Pattern black.' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B000900040000000900000000000000000017000B000900040000000999996666000000000061000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'drawNodeFirst:') protocolCategory: #'drawing'.! (DemoLib@#WireList methodAt: #'drawNodeFirst:') description: ('Draws the node shape representing the first point selected.').! DemoLib@#WireList compileMethodSource: ( Text from: 'distance: indx to: p " compute the Euclidean distance between points" ^( ((self at: indx) x - p x) squared + ((self at: indx) y - p y) squared) squareRoot' styleRuns:((ScrapStyle basicNew: 0) storageSize: 62; storageFromHexString: '000300000000000B00090004000000090000000000000000001A000B00090004000000099999666600000000004A000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'distance:to:') protocolCategory: #'calculating'.! (DemoLib@#WireList methodAt: #'distance:to:') description: ('Computes Euclidean distance between points. This really belongs in the class Co-ordinate. It is included hereonly to keep the demo self contained.').! DemoLib@#WireList compileMethodSource: ( Text from: 'drawRubberBandWith: index on: wPort "This method is used to draw ''rubber band'' representations when user moves a point" | oldMode oldColor p1 p2 m1 m2 | "make sure the right window port is at top of the thread stack; we will be changing pen colors in this method" thread pushGPort: wPort. " draw the wires and nodes" ((self size) > 0) ifFalse: [ ^nil ]. self drawOn: wPort. "is this the first point? if not set the temporary point (p1) to the one just before the one at position, index" (index = 1) ifTrue: [ p1 := self at: 1 ] ifFalse: [ p1 := self at: (index - 1) ]. "is this the last point? if not set the temporary point (p1) to the one just after the one at position, index" (index = (self size)) ifTrue: [ p2 := self at: (self size) ] ifFalse: [ p2 := self at: (index + 1) ]. "save the pen mode and color so we can restore them later" oldMode := wPort penMode. oldColor := wPort penFgColor. "set the pen to red; draw a red line from p1 to point specified by index to p2" wPort penFgColor: Color red. wPort movePenTo: p1. wPort drawLineToX: ((self at: index) x) Y: ((self at: index) y). self drawNode: (self at: index). wPort drawLineToX: (p2 x) Y: (p2 y). "change the pen to blue while the point is being dragged" wPort penFgColor: Color blue. wPort penMode: #patXor. "<-- try commenting this out to see what happens" "set m1 to local mouse position but don''t do anything while the user just holds the button down but not moving" [ m1 := Mouse localPosition. (m1 = Mouse localPosition) and: (Mouse isButtonDown) ] whileTrue: []. "while the button is down..." [Mouse buttonsDown] whileTrue: [ "if the mouse is on the first or last point, draw a line from that point to new mouse position" (index = 1) ifTrue: [ p1 := m1 ]. (index = (self size)) ifTrue: [ p2 := m1 ]. wPort movePenTo: p1. wPort drawLineToX: (m1 x) Y: (m1 y). self drawNode: m1. wPort drawLineToX: (p2 x) Y: (p2 y). "do nothing if mouse is not moved" [ m2 := Mouse localPosition. (m1 = m2) and: (Mouse isButtonDown) ] whileTrue: []. "draw the line each time the mouse moves, and set m1 to local position; this drawing is done in Xor pattern to produce a clean line for each move" wPort movePenTo: p1. wPort drawLineToX: (m1 x) Y: (m1 y). self drawNode: m1. wPort drawLineToX: (p2 x) Y: (p2 y). (Mouse isButtonDown) ifTrue: [ m1 := m2 ]. ]. "draw the new node position and lines when the button is released" wPort movePenTo: p1. wPort drawLineToX: (m1 x) Y: (m1 y). self drawNode: m1. wPort drawLineToX: (p2 x) Y: (p2 y). "reset the penMode, color and pop the GPort" wPort penMode: oldMode. wPort penFgColor: oldColor. thread popGPort. ' styleRuns:((ScrapStyle basicNew: 0) storageSize: 662; storageFromHexString: '002100000000000B000900040000000900000000000000000029000B00090004000000099999666600000000007C000B0009000400000009000000000000000000A8000B00090004000000099999666600000000011A000B000900040000000900000000000000000141000B00090004000000099999666600000000015C000B0009000400000009000000000000000001B6000B00090004000000099999666600000000022B000B000900040000000900000000000000000297000B00090004000000099999666600000000030A000B00090004000000090000000000000000038B000B0009000400000009999966660000000003C5000B00090004000000090000000000000000040E000B00090004000000099999666600000000045D000B000900040000000900000000000000000533000B00090004000000099999666600000000056C000B0009000400000009000000000000000005AB000B0009000400000009999966660000000005DC000B0009000400000009000000000000000005E6000B00090004000000099999666600000000065A000B0009000400000009000000000000000006DA000B0009000400000009999966660000000006F7000B000900040000000900000000000000000735000B0009000400000009999966660000000007A0000B0009000400000009000000000000000008C2000B0009000400000009999966660000000008E4000B00090004000000090000000000000000095D000B0009000400000009999966660000000009FC000B000900040000000900000000000000000AF9000B000900040000000999996666000000000B3B000B000900040000000900000000000000000BCD000B000900040000000999996666000000000BF9000B0009000400000009000000000000'))! (DemoLib@#WireList methodAt: #'drawRubberBandWith:on:') protocolCategory: #'drawing'.! (DemoLib@#WireList methodAt: #'drawRubberBandWith:on:') description: ('Method to draw rubber banding of wires. Used to move points around interactively.').!